package com.androidhelper.module.common.http.control;


import android.app.ProgressDialog;
import android.content.Context;

/*
*
*   基础数据的返回加入Dialog封装
*
* */
public abstract class ProgressObserver <T> extends BaseObserver<T> {

    private ProgressDialog progressDialog;
    private Context mContext;
    private String mLoadingText;



    public ProgressObserver(Context context){
        this(context,null);
    }

    public ProgressObserver(Context context,String loadingText){
        this.mContext=context;
        this.mLoadingText=loadingText;
        initProgress(context,loadingText);
    }

    private void initProgress(Context context,String loadingText) {
        if(progressDialog==null){
            progressDialog = new ProgressDialog(context);
        }
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage(mLoadingText == null ? "正在加载中..." : loadingText);

    }


    @Override
    public void onStart() {
        super.onStart();
        showProgress();
    }

    private void showProgress() {
        if(progressDialog!=null){
            if(!progressDialog.isShowing()){progressDialog.show();}

        }
    }



    /*@Override
    public void onSubscribe(Disposable disposable) {
        if(!disposable.isDisposed()){
            progressDialog = new ProgressDialog(mContext);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setMessage(mLoadingText == null ? "正在加载中..." : mLoadingText);
            progressDialog.show();
        }
    }*/


    @Override
    public void onComplete() {
        super.onComplete();
        dismissProgress();


    }



    @Override
    public void onError(Throwable e) {
        super.onError(e);
        dismissProgress();
    }


    private void dismissProgress() {
        if (progressDialog != null) {
           if(progressDialog.isShowing()) progressDialog.dismiss();
        }
    }






}
























































